package com.greatwideweb.sendtoself.sqlite;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;


import com.greatwideweb.sendtoself.sqlite.DBHelper;

public class DBAdapter {
	
	private Context context;
  private SQLiteDatabase database;
  private DBHelper dbHelper;
  private static final String FETCH_EMAIL_SQL="select email from email";
  
  public DBAdapter(Context context) {
    this.context = context;
  }
  
  public DBAdapter open() throws SQLException {
    dbHelper = new DBHelper(context);
    database = dbHelper.getWritableDatabase();
    return this;
  }
  
  public void close() {
    dbHelper.close();
  }
  
  public void truncateEmail() {
  	database.execSQL("delete from email");
  }
  
  public void saveEmail(String arg) {
  	truncateEmail();
  	ContentValues values=new ContentValues();
    values.put("email", arg);
    database.insert("email", null, values);
  }
  
  public String fetchEmail() {
  	Cursor c = database.rawQuery(FETCH_EMAIL_SQL, null);
    c.moveToFirst();
    String result=null;
    while (c.isAfterLast() == false) {
        result= c.getString(0);
        c.moveToNext();
        break;
    }
    c.close();
    return result;
  }
  

}
